﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MINERP.COM;

namespace MINERP.DAL.OverOrder
{
    public class OutShopDAL : DALBase
    {
        public OutProcessShop GetOneById(int outShopId)
        {
            return DataEntites.OutProcessShop.FirstOrDefault(p => p.OutShopId == outShopId);
        }
        
        public Shop GetOneShopById(int shopId)
        {
            return DataEntites.Shop.FirstOrDefault(p => p.ShopId == shopId);
        }

        /// <summary>
        /// 根据orderProductId得到对应的产品属于哪些加工厂加工的
        /// </summary>
        /// <param name="orderProductId"></param>
        /// <returns></returns>
        public List<OutProcessShop> getShopByOrderProductId(int orderProductId)
        {
            List<OutProcessShop> list = new List<OutProcessShop>();
            orderInfo oi = DataEntites.orderInfo.Where(p => p.OrderProductId == orderProductId).First();
            if (!oi.Tb_ProductReference.IsLoaded)
            {
                oi.Tb_ProductReference.Load();
            }
            if (!oi.Tb_Product.Tb_OutShop_ProductCategory.IsLoaded)
            {
                oi.Tb_Product.Tb_OutShop_ProductCategory.Load();
            }

            foreach (var item in oi.Tb_Product.Tb_OutShop_ProductCategory)
            {
                if (!item.OutProcessShopReference.IsLoaded)
                {
                    item.OutProcessShopReference.Load();
                }

                list.Add(item.OutProcessShop);
            }
            return list;
        }

        /// <summary>
        /// 得到所有的外加工商,并按委外商名称排序
        /// </summary>
        /// <returns></returns>
        public List<OutProcessShop> GetAllOutShop()
        {
            return DataEntites.OutProcessShop.OrderBy(op => op.OutShopName).ToList();
        }
        public List<OutProcessRecord> GetOutShopInGridRecord(int inOutShopId)
        {
            int inGrid = (int)OutProcessRecordStatus.InGrid;
            return DataEntites.OutProcessRecord.Where(p => p.OutProcessShop.OutShopId == inOutShopId && p.Status == inGrid).ToList();;
        }
    }
}
